<template lang="html">
  <div class="changePass" :exit="exit">
    <el-form
      :model="ruleForm2"
      status-icon
      :rules="rules2"
      ref="ruleForm2"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="原密码" prop="oldPass">
        <el-input type="password" v-model="ruleForm2.oldPass"></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="pass">
        <el-input
          type="password"
          v-model="ruleForm2.pass"
          auto-complete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass">
        <el-input
          type="password"
          v-model="ruleForm2.checkPass"
          auto-complete="off"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm2')"
          >提交</el-button
        >
        <el-button @click="resetForm('ruleForm2')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import axios from "axios";
import qs from "qs"; //配合axios解析url将url中的参数转为对象；将对象转为url参数形式
import { Message, MessageBox } from "element-ui";

export default {
  watch: {
    //深度监听一个对象的某个值发生变化
    "ruleForm2.oldPass": {
      deep: true,

      handler(val, oldval) {
        //发起请求验证输入的原密码是否存在
        axios
          .post("/v1/users/checkpassword", `password=${val}`)
          .then((result) => {
            this.status = result.data.status;
          })
          .catch((err) => {
            console.log(err);
          });
      },
    },
  },
  data() {
    var checkOldPass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入原密码"));
      } else if (this.status != 200) {
        callback(new Error("原密码错误"));
      } else {
        callback();
      }
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else if (value.length < 8 || /^\d+$/.test(value)) {
        callback(new Error("密码不能为纯数字,且不能小于8位"));
      } else {
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm2.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      status: 0, //存后台发过来的状态码，如果状态码=200则表示成功，也就是原密码存在
      ruleForm2: {
        pass: "",
        checkPass: "",
        oldPass: "",
      },
      rules2: {
        pass: [{ validator: validatePass, trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }],
        oldPass: [{ validator: checkOldPass, trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          axios({
            method: "put",
            url: "/v1/users/updatepassword",
            data: qs.stringify({
              password: this.ruleForm2.oldPass,
              newpassword: this.ruleForm2.pass,
            }),
            headers: {
              "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
            },
          })
            .then((result) => {
              if (result.data.status == 200) {
                MessageBox("密码修改成功，请重新登陆", "提示", {
                  confirmButtonText: "确定",
                  type: "success",
                });
                this.exit();
              } else {
                MessageBox("密码修改失败，新密码不能跟原密码一致", "提示", {
                  confirmButtonText: "确定",
                  type: "error",
                });
              }
            })
            .catch((err) => {
              console.log(err);
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
  props: ["exit"],
};
</script>

<style lang="css">
.changePass {
  width: 60%;
  margin: 0 auto;
  margin-top: 50px;
}
</style>
